﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Data.SqlClient;
using System.Text;
using System.Threading.Tasks;
using System.Data;

namespace dreametryVersion2
{
    public static class chat
    {

        public static int cmbIndex = 0;
        public static String msgForAll;
        public static string GetLocalIP()
        {
            IPHostEntry host;
            host = Dns.GetHostEntry(Dns.GetHostName());

            foreach (IPAddress ip in host.AddressList)
            {
                if (ip.AddressFamily == AddressFamily.InterNetwork)
                {
                    return ip.ToString();
                }
            }

            return "192.168.1.100";
        }

        public static void updateipAddress()
        {
            string commandText = "UPDATE employee SET pc_ip_address = @ip WHERE emp_id = @id";

            using (SqlConnection connection = new SqlConnection(global.conString))
            {
                SqlCommand command = new SqlCommand(commandText, connection);
                command.CommandType = CommandType.Text;
                command.Parameters.AddWithValue("@id", global.employeeID);
                command.Parameters.AddWithValue("@ip", chat.GetLocalIP());

                try
                {
                    command.Connection.Open();
                    command.ExecuteNonQuery();
                }
                catch
                {

                }
                finally
                {
                    command.Connection.Close();
                }


            }
        }

        public static string getAdminIP()
        {
            string commandText = "SELECT employee.pc_ip_address FROM employee Inner join RoleTable on employee.emp_id = RoleTable.emp_id WHERE AdminChat LIKE 'y'";

            using (SqlConnection connection = new SqlConnection(global.conString))
            {
                SqlCommand command = new SqlCommand(commandText, connection);
                try
                {
                    command.Connection.Open();
                   return command.ExecuteScalar().ToString();
                }
                catch
                {
                    return "";

                }
                finally
                {
                    command.Connection.Close();
                }



            }
        }


    }
}

